package com.woniuxy.credit_loans.staff_ma.controller;


import com.woniuxy.credit_loans.annotation.OperationLog;
import com.woniuxy.credit_loans.entity.User;
import com.woniuxy.credit_loans.staff_ma.dto.UserDto;
import com.woniuxy.credit_loans.staff_ma.dto.UserDtoT;
import com.woniuxy.credit_loans.staff_ma.service.LoginLoggerService;
import com.woniuxy.credit_loans.staff_ma.service.UserService;
import com.woniuxy.credit_loans.staff_ma.vo.UserVo;
import com.woniuxy.credit_loans.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "用户个人操作对应接口")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private LoginLoggerService loggerService;

    @ApiOperation(value = "用户登录")
    @PostMapping("loginDo")
    @OperationLog
    public Result login(@RequestBody UserDto dto){
        User login = userService.login(dto);
        if (login != null && login.getUserOpen() == 1) {
            return Result.ok().data("user",login);
        } else {
            return Result.error();
        }
    }

    @GetMapping("/Bona")
    private  Result selectUserName(){
         List<User> users = userService.selectAll();
         return  Result.ok().data("users",users);
    }


    @ApiOperation(value = "用户注册")
    @PostMapping("registerDo")
    @OperationLog
    public Result register(@RequestBody User user){
        int register = userService.register(user);
        if(register == 0){
            return Result.error("账号重复，请重新输入");
        } else if(register == 2) {
            return Result.error("两次密码输入不一致，请重新输入");
        } else {
            return Result.ok("注册成功");
        }
    }

    @ApiOperation(value = "根据id查询用户信息")
    @GetMapping("{userId}")
    public Result queryUserInfo(@PathVariable Long userId){
        User user = userService.selectByPrimaryKey(userId);
        return Result.ok().data("userInfo",user);
    }

    @GetMapping("/queryByUser")
    public Result selectByUser(@RequestBody User user){
        User user2 = userService.selectByUser(user);
        return Result.ok().data("queryByUser",user2);
    }

    @ApiOperation(value = "用户密码更改")
    @PostMapping("/userid")
    @OperationLog
    public Result updateUserPwd(@RequestBody UserDtoT userDtoT){
        User user = new User();
        user.setUserPassword(userDtoT.getUserPassword());
        user.setUserId(userDtoT.getUserId());
        int i = userService.updateByPrimaryKeySelective(user);
        return Result.ok();
    }

    @ApiOperation(value = "用户登录后查询贷款记录")
    @GetMapping("")
    public Result checkBorrowHistory(@RequestBody UserVo userVo){
        UserVo userVo1 = userService.selectBorrowHistory(userVo);
        if (userVo1 != null) {
            return Result.ok().data("userBorrowHistory",userVo1);
        }else {
            return null;
        }
    }

       @ApiOperation(value ="更新/完善用户信息")
       @PutMapping("")
       @OperationLog
       public Result updateUserInfo(@RequestBody User user){
            int i = userService.updateByPrimaryKeySelective(user);
            if (i>0) {
                return  Result.ok().data("updateUser",user);
            }else {
                return Result.error();
            }
        }



}
